Skip to main content

1518. Water Bottles

Easy
Description

There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

Example 1:

Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 9 + 3 + 1 = 13.

Example 2:

Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 15 + 3 + 1 = 19.

Constraints:

  • 1 <= numBottles <= 100
  • 2 <= numExchange <= 100

解題思路

題目要算出最多能喝到幾瓶水,兩個參數 numBottlesnumExchange,代表一開始總共有 numBottles 瓶滿的水,且用 numExchange 個空瓶就可以換到一瓶滿的水。

我的做法是先定義兩個變數

let result = 0; //總共喝了幾瓶水
let emptyBottles = 0; //目前的空瓶數量

第一步是使用 while 迴圈,當還有水沒喝光的時候就重複執行程式

while (numBottles > 0) {}

迴圈中首先是先把所有滿的水喝光

while (numBottles > 0) {
result += numBottles; //喝光一開始的 numBottles 瓶水,之後加進 result 中
emptyBottles += numBottles; //喝光一開始的 numBottles 瓶水,所以多了 numBottles 個空瓶
}

之後加上使用空瓶換水的部分

while (numBottles > 0) {
result += numBottles;
emptyBottles += numBottles;
numBottles = Math.floor(emptyBottles / numExchange); // 使用目前的空瓶換水
emptyBottles = emptyBottles % numExchange; // 算出換完剩下的空瓶總數
}

最後當水喝完且空瓶不夠換水的時候就是答案了!

心得

先把題目看一遍,先寫出 sudo code,之後嘗試把 sudo code 轉為真實程式碼。